package com.itheima.sc.impl.sms;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sc.api.sms.SmsCouponHistoryApi;
import com.itheima.sc.mapper.sms.SmsCouponHistoryMapper;
import com.itheima.sc.pojo.sms.Coupon;
import com.itheima.sc.pojo.sms.CouponHistory;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsCouponHistoryImpl implements SmsCouponHistoryApi {

    @Autowired
    private SmsCouponHistoryMapper couponHistoryMapper;

    @Override
    public PageResult couponHistoryList(Integer page, Integer pageSize, Integer useStatus, Integer couponId, String orderSn) {

        //构建分页查询条件
        Page<CouponHistory> ipage = new Page<>(page, pageSize);
        //构建条件查询
        LambdaQueryWrapper<CouponHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(useStatus != null, CouponHistory::getUseStatus, useStatus);
        wrapper.eq(couponId != null, CouponHistory::getCouponId, couponId);
        wrapper.like(StrUtil.isNotBlank(orderSn), CouponHistory::getOrderSn, orderSn);

        //查询请求
        ipage = couponHistoryMapper.selectPage(ipage, wrapper);
        return new PageResult(page, pageSize, Convert.toInt(ipage.getTotal()),
                Convert.toInt(ipage.getTotal() / pageSize), ipage.getRecords());
    }
}
